package com.ruoyi.deeplearn.service;

import com.ruoyi.deeplearn.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DatasetMarkV2Service {
    @Autowired
    private IDeepDatasetFileService deepDatasetFileService;
    @Autowired
    private IDeepDatasetMarkService deepDatasetMarkService;
    @Autowired
    private IDeepDatasetService deepDatasetService;


    public int saveMark(Long fileId, List<MarkTagVo> markTagVos) {
        if(fileId==null||markTagVos.isEmpty())return 0;
        DeepDatasetFile deepDatasetFile = deepDatasetFileService.selectDeepDatasetFileByFileId(fileId);
        if(deepDatasetFile==null)return 0;
        DeepDataset deepDataset = deepDatasetService.selectDeepDatasetById(deepDatasetFile.getDatasetId());
        if(deepDataset==null)return 0;
        markTagVos.stream().forEach(item->{
            DeepDatasetMark mark = new DeepDatasetMark();
            mark.setModuleId(deepDatasetFile.getModuleId());
            mark.setObjectName(deepDatasetFile.getObjectName());
            mark.setDatasetId(deepDatasetFile.getDatasetId());
            mark.setMarkStatus(0);
            mark.setAngle(item.getAngle());
            mark.setAlias(item.getAlias());
            mark.setTagId(item.getTagId());
            mark.setColor(item.getColor());
            mark.setOrder(item.getOrder());
            mark.setMarkType(item.getMarkType());
            mark.setPath(item.getPath());
            mark.setFileId(fileId);
            deepDatasetMarkService.insertDeepDatasetMark(mark);
        });
        //更新标记信息
        //计算标记次数和标记成功
        List<DatasetFileTag> tagList = deepDatasetMarkService.groupFileTagByFileId(fileId);
        if(tagList!=null&&tagList.size()>0){
            if(tagList.size()==1){
                deepDatasetFile.setMarkCount(tagList.get(0).getCount());
                //标记成功：标记次数大于等于需要标记次数
                if(deepDatasetFile.getMarkCount().intValue()>=deepDatasetFile.getMarkNeedCount().intValue()){
                    deepDatasetFile.setMarkStatus(1);//完成标记
//                    deepDatasetFile.setMarkId(tagList.get(0).get);
                    deepDatasetFile.setMarkTagId(tagList.get(0).getTagId());
                }
                deepDatasetFileService.updateDeepDatasetFile(deepDatasetFile);
            }else {//一个文件标记多余一个
                int sum = tagList.stream().mapToInt(DatasetFileTag::getCount).sum();
                deepDatasetFile.setMarkCount(sum);
                //根据排序，判断最多标记的是否满足了
                List<DatasetFileTag> sortedTags = tagList.stream().sorted(Comparator.comparing(DatasetFileTag::getCount)).collect(Collectors.toList());
                DatasetFileTag maxTags = sortedTags.get(0);
                if(maxTags.getCount().intValue()>=deepDatasetFile.getMarkNeedCount().intValue()){
                    deepDatasetFile.setMarkStatus(1);//完成标记
                    deepDatasetFile.setMarkTagId(maxTags.getTagId());
                }
                deepDatasetFileService.updateDeepDatasetFile(deepDatasetFile);
            }
            //更新数据集的标记数量
            Long totalMarked = deepDatasetFileService.countDatasetMarked(deepDatasetFile.getDatasetId());
            deepDataset.setMarkCount(totalMarked);
            deepDatasetService.updateDeepDataset(deepDataset);
        }
        return markTagVos.size();
    }
}
